package top.oauth2.api.demo.util;

import com.alibaba.excel.EasyExcel;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import top.xphu.zip.service.DownloadFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Map;

/**
 * @author xphu
 * @version 1.0
 * @date 2021/9/2 17:58
 */
public class TempExcelUtil {


    public void downloadExcel(String tempExcelPath, String newFileName, Map<String, Object> data, HttpServletResponse response) throws Exception {
        // 获取模板文件
        File templateFile = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + tempExcelPath);
        System.out.println(templateFile.getParentFile().getPath());
        String fileName = (StringUtils.isEmpty(newFileName) ? System.currentTimeMillis() : newFileName )+ ".xlsx";
        String filePath = templateFile.getParentFile().getAbsolutePath()  + fileName;
        EasyExcel.write(filePath).withTemplate(templateFile).sheet().doFill(data);
        DownloadFile.build().download(new File(filePath), fileName, response);
    }
}
